/*
 * ChannelList.java
 *
 * Created on July 28, 2007, 9:54 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.afaker.rss.feed;

import com.afaker.rss.context.ApplicationContext;
import com.afaker.rss.xml.XMLDocumentFactory;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.jdom.Document;
import org.jdom.Element;

/**
 *
 * @author bruce
 */
public class ChannelList {

    private HashSet list;
    private XMLDocumentFactory document = null;
    private Document doc;
    private String url;
    private ApplicationContext appContext = ApplicationContext.getApplicationContext();
    public static final String CHANNEL_NODE_NAME = "channel";
    public static final String FEEDURI_NODE_NAME = "feeduri";

    /**
     * Creates a new instance of ChannelList
     */
    public ChannelList(String url) {
        list = new HashSet();
        this.url = url;
        document = new XMLDocumentFactory();
        doc = document.build(url);
        readList();
    }

    public Set readList() {

        Element root = doc.getRootElement();
        Iterator it = root.getChildren(CHANNEL_NODE_NAME).listIterator();
        Element next;
        System.out.println(appContext.getProperty("channellist.reading"));
        while (it.hasNext()) {
            next = (Element) it.next();
            list.add(next.getChild(FEEDURI_NODE_NAME).getText());
        }
        return list;
    }

    public Set getList() {
        return list;
    }

    public boolean addChannelToList(String uri) {
        if (contains(uri)) {
            System.out.println(appContext.getProperty("channel.add.duplicate") + uri);
            return false;
        }
        Element channel = new Element(CHANNEL_NODE_NAME);
        Element feedURI = new Element(FEEDURI_NODE_NAME);
        feedURI.setText(uri);
        channel.addContent(feedURI);
        Element root = doc.getRootElement();
        root.addContent(channel);
        if (XMLDocumentFactory.outputToFile(doc, url)) {
            System.out.println(appContext.getProperty("channel.add.success") + uri);
            return true;
        } else {
            System.out.println(appContext.getProperty("channel.add.failure"));
            return false;
        }

    }

    public void removeChannel(String uri) {
        list.remove(uri);
        Element toRemove;
        Element root = doc.getRootElement();
        Iterator it = root.getChildren(CHANNEL_NODE_NAME).listIterator();
        while (it.hasNext()) {
            toRemove = (Element) it.next();
            if (toRemove.getChild(FEEDURI_NODE_NAME).getText().equals(uri)) {
                root.removeContent(toRemove);
                break;
            }
        }
        if (XMLDocumentFactory.outputToFile(doc, url)) {
            System.out.println(appContext.getProperty("channel.delete.success"));
        } else {
            System.out.println(appContext.getProperty("channel.delete.failure"));
        }
    }

    public boolean contains(String uri) {
        return list.contains(uri);
    }

    public static void main(String args[]) {
        ChannelList list = new ChannelList("src/channellist.xml");
        list.addChannelToList("www.baidu");
        list.addChannelToList("google");
        list.addChannelToList("google");
        Set a = list.readList();
        Iterator it = a.iterator();
        while (it.hasNext()) {
            System.out.println("List has:" + it.next());
        }
    }
}
